package com.hs.pre.module.data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.hs.core.db.DBOperator;
import com.hs.core.db.DatabaseConnection;
import com.hs.log.HsLogger;
import com.hs.pre.model.SortBean;
import com.hs.pre.model.SortSimBean;


/**
 * 方剂-分类关系缓存
 * @author Administrator
 *
 */
public class FjPreCache {
	private static final HsLogger logger = HsLogger.getLogger(DataGatherAction.class.getName());
	private static FjPreCache  fjPreCache = null;
	
	/**
	 * 分类与方剂 缓存
	 * key 分类id
	 * value PreBean *
	 */
	private Map  fjPreRe ;
	
	
	/**
	 * 方剂与分类缓存
	 * key 方剂code
	 * value 分类id
	 */
	private Map fjSort;

	/**
	 * 方剂分类上下级 缓存
	 * key 上级id
	 * value key下的分类id
	 */
	private Map fjReMap;
	
	
	/**
	 * 方剂信息
	 * key 方剂id
	 * value 方剂bean SortSimBean
	 */
	private Map sortCache;
	
	
	private FjPreCache()
	{
		this.init();
	}
	
	public static FjPreCache getInstance()
	{
		if(fjPreCache == null)
			fjPreCache= new FjPreCache();
		return fjPreCache;
	}
	
	/***
	 *  初始化
	 *  初始方剂 分类关系
	 *  
	 */
	private void init()
	{
		
		fjPreRe();
		fjRe();
	}
	
	/****
	 * 缓存分类
	 */
	private void fjRe()
	{
		String sql = "select parentsortid,sortId,sortName from app.PRE_SORT";
		DBOperator dbo = null;
		try {
			dbo = DatabaseConnection.getDBOperator();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (dbo == null)
			return ;
		if(fjReMap == null)
			fjReMap = new HashMap();
		else fjReMap.clear();
		if(sortCache == null)
			sortCache = new HashMap();
		else sortCache.clear();
		
		
		List list = dbo.select(sql,null);
		Iterator iterator = list.iterator();
		while(iterator.hasNext())
		{
			Map row = (Map)iterator.next();
			Long sortId = (Long)row.get("sortId");
			Long parentsortid = (Long)row.get("parentsortid");
			String sortName = (String)row.get("sortName");
			SortSimBean sortSimBean= new SortSimBean();
			sortSimBean.setSortName(sortName);
			sortSimBean.setPerId(parentsortid);
			sortSimBean.setSortId(sortId);
			sortCache.put(sortId, sortSimBean);
			if(fjReMap.containsKey(parentsortid))
			{
				((List)fjReMap.get(parentsortid)).add(sortId);
			}
			else
			{
				List preList = new ArrayList();
				preList.add(sortId);
				fjReMap.put(parentsortid, preList);
			}
		}
	}
	
	/**
	 * 分类和方剂的关系缓存
	 */
	private void fjPreRe()
	{
//		String sql ="select a.preid,a.id,b.sortid from app.PRE_INFO a LEFT JOIN app.pre_sort_relation b  ON a.preid=b.preid";
		String sql ="select a.preid,a.id,b.sortid  from app.PRE_INFO  a,app.pre_sort_relation b where a.preid=b.preid";
		DBOperator dbo = null;
			try {
				dbo = DatabaseConnection.getDBOperator();
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (dbo == null)
				return ;
		if(fjPreRe == null)
			fjPreRe = new HashMap();
		else fjPreRe.clear();
		
		
		if(fjSort == null)
			fjSort = new HashMap();
		else fjSort.clear();
		
		List list = dbo.select(sql,null);
		Iterator iterator = list.iterator();
		while(iterator.hasNext())
		{
			Map row = (Map)iterator.next();
			Long sortId = (Long)row.get("sortId");
//			PreBean preBean = new PreBean();
//			preBean.setCode((String)row.get("id"));
//			preBean.setPreId((Long)row.get("preid"));
			String code = (String)row.get("id");
			fjSort.put(code, sortId);
			if(fjPreRe.containsKey(sortId))
			{
				((List)fjPreRe.get(sortId)).add(code);
			}
			else
			{
				List preList = new ArrayList();
				preList.add(code);
				fjPreRe.put(sortId, preList);
			}
		}
		
	}

	/**
	 * 获得该分类下面的所有方剂id
	 * 
	 * @param sortId 分类id
	 * @return
	 */
	public String getPreStrId(Long sortId)
	{
		List listSort = new ArrayList();
		this.getChild(listSort, sortId);
		StringBuffer strPreId = new StringBuffer("");
		
		int i = 0;
		Iterator iteratorSortList = listSort.iterator();
		while(iteratorSortList.hasNext())
		{
			List  list = (List) this.fjPreRe.get((Long)iteratorSortList.next());
			if(list != null)
			{
				Iterator iterator = list.iterator();
				while(iterator.hasNext())
				{
//					PreBean preBean = (PreBean)iterator.next();		
//					strPreId.append(" id="+preBean.getCode()+" or");
					strPreId.append(" id:"+iterator.next()+" OR");
					i++;
				}
			}
		}
		logger.info("#################方剂条数:"+i);
		if(strPreId.length()>2)
		{
			strPreId.deleteCharAt(strPreId.length()-1);
			strPreId.deleteCharAt(strPreId.length()-1);
		}
		return strPreId.toString();
	}
	
	/**
	 * 获得分类下面的所有子分类
	 * @param sortId
	 * @return
	 */
	private void getChild(List listSort,Long sortId)
	{
		listSort.add(sortId);
		List fj = (List)fjReMap.get(sortId);
		if(fj != null)
		{
			Iterator iterator = fj.iterator();
			while(iterator.hasNext())
			{
				this.getChild(listSort, (Long)iterator.next());
			}
		}
	}
	/**
	 * 获得分类下面的所有子分类
	 * @param sortId
	 * @return
	 */
	private void getChild(Set set,Long sortId)
	{
		set.add(sortId);
		List fj = (List)fjReMap.get(sortId);
		if(fj != null)
		{
			Iterator iterator = fj.iterator();
			while(iterator.hasNext())
			{
				this.getChild(set, (Long)iterator.next());
			}
		}
	}
	
	
	/**
	 * 通过方剂id数组找到相应的分类,及其上下级分类
	 * @param fjIds
	 * @return
	 */
	public List findSort(String[] fjIds)
	{
		System.out.println("---------方剂个数:"+fjIds.length);
		int size = fjIds.length;
		Set set = new HashSet();
		for(int i=0;i<size;i++)
		{
			Long sortId = (Long)fjSort.get(fjIds[i]);
			if(sortId != null)
				if(set.add(sortId))
				{
					/********下级所有*******/
					this.getChild(set, sortId);
					/********上级所有*******/
					this.setParentSort(set, sortId);	
				}
		}
		
		/**
		 * 找出分类信息
		 */
		List list = new ArrayList();
		Iterator iterator = set.iterator();
		while(iterator.hasNext())
		{
			Long sortId = (Long)iterator.next();
			list.add(sortCache.get(sortId));
		}
		return list;
	}
	
	/**
	 * 找出该分类的所有父级
	 * @param set
	 * @param sortId
	 */
	private void setParentSort(Set set ,Long  sortId)
	{
		SortSimBean sortSimBean = (SortSimBean)sortCache.get(sortId);
	
		if(sortSimBean != null)
		{
			if(sortSimBean.getPerId() != null)
			{
				SortSimBean sortSimBeanTem = (SortSimBean)sortCache.get(sortSimBean.getPerId());
				if(sortSimBeanTem != null)
				{
					if(set.add(sortSimBean.getPerId()))
						this.setParentSort(set, sortSimBean.getPerId());
				}
				
			}
		}
	}
	
	
	/**
	 * 通过散列的方剂编码找出分类树
	 * @param code
	 */
	private void getSortTree(String code)
	{
		
	}

//	
//	class PreBean
//	{
//		private Long preId;
//		/**
//		 * 方剂对于检索文件id
//		 * 
//		 */
//		private String code;
//		public Long getPreId() {
//			return preId;
//		}
//		public void setPreId(Long preId) {
//			this.preId = preId;
//		}
//		public String getCode() {
//			return code;
//		}
//		public void setCode(String code) {
//			this.code = code;
//		}
//		
//	}
}
